#include "ukuiwindowpicker.h"
#include "ukuiwindow.h"
#include "ukuiwindowmanager.h"

#include <QDebug>
#include <QMouseEvent>
#include <QPainter>
#include <QPen>

namespace ukuiwaylandclient
{
UkuiWindowPicker::UkuiWindowPicker(UkuiWindowManager *windowManager)
    : m_windowManager(windowManager)
{
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setProperty("ukui_surface_role", "onscreendisplay");
    setCursor(Qt::CrossCursor);
    setMouseTracking(true);

    windowManager->start();
}

UkuiWindowPicker::~UkuiWindowPicker()
{
}

void UkuiWindowPicker::mouseMoveEvent(QMouseEvent *event)
{
    m_cursorPos = event->pos() + pos();

    update();
}

void UkuiWindowPicker::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        Q_EMIT windowSelected(event->pos());
    }
}

void UkuiWindowPicker::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    UkuiWindow *window = m_windowManager->findWindowOnPosition(m_cursorPos);
    if (window) {
        QPainter painter(this);
        QPen pen;
        pen.setColor(Qt::red);
        pen.setWidth(2);
        painter.setPen(pen);
        painter.drawRect(window->geometry());
    }
}

} // namespace ukuiwaylandclient
